package com.spring.controller.web.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.spring.controller.web.UserThreadLocal;

/**
 * 解决2次查询User的问题（ThreadLocal）
 * 原始传统解决方式中【不优雅，不专业】：
 * 	1.在拦截器中判断一次登陆，set到request对象中
 *  2.在Controller中通过request获取user
 * @author:chenhs E-mail:hson08_chen@163.com
 * Created on 2018年3月6日 上午11:11:47
 */
public class UserLoginHandlerInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		
		UserThreadLocal.set(null);	//清空当前线程中的User对象
	/*	
		String token = CookieUtils.getCookieValue(request, COOKIE_NAME);
		if(StringUtils.isEmpty(token)) {
			//未登录状态
			response.sendRedirect(loginUrl);
			return false;
		}
		
		User user = this.userService.queryUserByToken(token);
		if(null == user) {
			response.sendRedirect(loginUrl);
			return false;
		}*/
		
		//处于登录状态
	//	UserThreadLocal.set(user);	//将User对象放置到ThreadLocal中
		
		return true;
	}

}
